import Mock from 'mockjs'
import moment from 'moment'
// 获取实验列表
import exptList from './exptList'

Mock.mock('/api/getExptList', 'get', () => {
    const exptList1 = JSON.parse(localStorage.getItem('exptList')) || exptList
    return {
        code: 200,
        data: exptList1,
        msg: "获取实验列表成功"
    }
})
// 添加实验

Mock.mock('/api/addExpt', 'post', ({ body }) => {

    const exptList1 = JSON.parse(localStorage.getItem('exptList')) || exptList
    let data = JSON.parse(body)
    data.id = exptList1.length
    data.exptNum = exptList1.length
    data.createTime = moment(new Date()).format('YYYY-MM-DD HH:mm:ss')

    data.status = 0
    data.status1 = ''

    exptList1.push(data)
    localStorage.setItem('exptList', JSON.stringify(exptList1))
    return {
        code: 200,
        data: exptList,
        msg: "获取实验列表成功"
    }
})

// 获取get请求参数
const getQuery = (url, name) => {
    const index = url.indexOf('?')
    if (index !== -1) {
        const queryStrArr = url.substr(index + 1).split('&')
        for (let i = 0; i < queryStrArr.length; i++) {
            const itemArr = queryStrArr[i].split('=')
            if (itemArr[0] === name) {
                return itemArr[1]
            }
        }
    }
    return null
}

// 根据实验id获取实验用户
import exptUserList from './exptUserList'

Mock.mock(/\/api\/getUserListByExptId/, 'get', (options) => {

    const exptId = Number(getQuery(options.url, 'exptId'))
    const exptUserList1 = JSON.parse(localStorage.getItem('exptUserList')) || exptUserList
    let item = exptUserList1.filter(v => v.exptId === exptId)
  
    if (item && item.length) {
        return {
            code: 200,
            data: item,
            msg: "获取实验用户列表成功"
        }
    } else {
        return {
            code: 300,
            data: [],
            msg: "获取实验用户列表失败"
        }
    }
})
// 根据实验id查找阵营
import campList from './campList'
Mock.mock(/\/api\/getCampListByExptId/, 'get', (options) => {

    const exptId = Number(getQuery(options.url, 'exptId'))
    const exptCampList1 = JSON.parse(localStorage.getItem('exptCampList')) || campList
    let item = exptCampList1.filter(v => v.exptId === exptId)
    if (item && item.length) {
        return {
            code: 200,
            data: item[0].list,
            msg: "获取实验阵营列表成功"
        }
    } else {
        return {
            code: 300,
            data: [],
            msg: "获取实验阵营列表失败"
        }
    }
})

